using UnityEngine;
using System.Collections;

public class Personagem : MonoBehaviour 
{
	private int id = 0;
	private Vector2 screen;
	
	private bool col = true;
	
	private AudioClip pulo;
	private AudioClip splash;

    void Start()
    {
        this.gameObject.AddComponent("MeshRenderer");
        this.gameObject.AddComponent("MeshFilter");
		this.gameObject.AddComponent("Rigidbody");
		this.gameObject.AddComponent("BoxCollider");

		this.screen.y = GameObject.Find("Main Camera").GetComponent<Camera>().orthographicSize;
		this.screen.x = this.screen.y * GameObject.Find("Main Camera").GetComponent<Camera>().aspect;

        Mesh mesh = Quad.Create(this.screen.y * 0.2f, this.screen.y * 0.2f);

        this.gameObject.GetComponent<MeshFilter>().mesh = mesh;
        this.gameObject.GetComponent<MeshRenderer>().material = AtlasPersonagem.globalMaterial;
		this.gameObject.GetComponent<BoxCollider>().size = new Vector3(this.screen.y * 0.2f, this.screen.y * 0.4f , 0.4f);
		
		this.transform.position = new Vector3(-this.screen.x + (this.screen.y * 0.4f), -2, 0);
		
		rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
		
		this.StartCoroutine(this.Animacao());
		
		this.gameObject.AddComponent("AudioSource");
		
		this.pulo = (AudioClip)Resources.Load("Audio/Pulando");
		this.splash = (AudioClip)Resources.Load("Audio/Splash");
		
		this.gameObject.audio.clip = this.pulo;
		
		GameObject.Find("Chao").GetComponent<Transform>().transform.position = new Vector3(this.transform.position.x, -2.5f, 0);
    }

    public void Update()
    {
        this.gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[]
		{
			new Vector2(AtlasPersonagem.rects[id].xMin, AtlasPersonagem.rects[id].yMax),
			new Vector2(AtlasPersonagem.rects[id].xMax, AtlasPersonagem.rects[id].yMax),
			new Vector2(AtlasPersonagem.rects[id].xMax, AtlasPersonagem.rects[id].yMin),
			new Vector2(AtlasPersonagem.rects[id].xMin, AtlasPersonagem.rects[id].yMin)
		};
		
		if(Input.GetKeyDown(KeyCode.UpArrow) && this.col == true)
		{
			this.gameObject.audio.Play();
			
			this.gameObject.rigidbody.AddForce(Vector3.up * 350);
		}
		
		if(this.gameObject.transform.position.x < - this.screen.x - (this.screen.y * 0.2f))
		{
			Application.LoadLevel("Perdeu");	
		}
    }
	
	void OnCollisionEnter(Collision c)
	{
		if(c.gameObject.tag != "Obstaculo")
		{
			this.col = true;
			StopAllCoroutines();
			StartCoroutine(Animacao());
		}
		
		if(c.gameObject.tag == "Obstaculo")
		{
			this.gameObject.rigidbody.AddForce(Vector3.left * 500);	
		}
	}
	
	void OnCollisionExit()
	{
		this.col = false;
		this.id = 3;
		StopAllCoroutines();
		StartCoroutine(Pulando());
	}
	
	IEnumerator Animacao()
	{
		while (true)
        {
			if(this.id > 2)
			{
				this.id = 0;	
			}
		
			yield return new WaitForSeconds(0.1f);
		
			this.id++;
		}
	}
	
	IEnumerator Pulando()
	{
		while (true)
        {
			if(this.id > 7)
			{
				this.id = 0;	
			}
		
			yield return new WaitForSeconds(0.3f);
		
			this.id++;
		}
	}
}
